package br.gov.sjc.adapter;

import java.util.List;

import br.gov.sjc.aluguelsocial.R.beneficio;
import br.gov.sjc.aluguelsocial.R.layout;
import br.gov.sjc.classes.Beneficio;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BeneficioAdapter extends BaseAdapter {
	
	private final String TAG = "BeneficioAdapter";
	private Context context;
	private List<Beneficio> lstBeneficio;
	private LayoutInflater BeneficioInflater;
	
	public BeneficioAdapter (Context context, List<Beneficio> listBeneficio){
		this.context = context;
		this.lstBeneficio = listBeneficio;
		BeneficioInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void notifyDataSetChanged(){
		try{
			super.notifyDataSetChanged();
		}catch(Exception e){
			Log.e(TAG,"notifyDataSetChanged E - "+e.getMessage());
		}
	}

	public int getCount() {
		return lstBeneficio.size();
	}
	
	public void remove(final Beneficio item){
		this.lstBeneficio.remove(item);
	}
	
	public void add(final Beneficio item){
		this.lstBeneficio.add(item);
	}

	public Object getItem(int position) {
		return lstBeneficio.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup viewGroup) {
		try{
			Beneficio beneficiovo = lstBeneficio.get(position);
			ViewHolder holder;
			if(convertView == null){
				convertView = BeneficioInflater.inflate(layout.beneficiotw, null);
				holder = new ViewHolder();
				
				holder.tvProgramaSocial = (TextView) convertView.findViewById(beneficio.tvProgramaSocial);
                holder.tvAno = (TextView) convertView.findViewById(beneficio.tvAno);
                holder.tvNome = (TextView) convertView.findViewById(beneficio.tvNome);
                holder.tvSituacao = (TextView) convertView.findViewById(beneficio.tvSituacao);
               convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
            // * COLOCA DADOS NA LISTVIEW
			
			holder.tvProgramaSocial.setText(beneficiovo.getPrograma());
            holder.tvAno.setText(beneficiovo.getAno());
            holder.tvNome.setText(beneficiovo.getCidadao());
            holder.tvSituacao.setText(beneficiovo.getSituacao());
            return convertView;
		}catch(Exception e){
			Log.e(TAG, "getView e - "+e.getMessage());
		}
		return convertView;
	}
    static class ViewHolder {
    	//campos
        public TextView tvProgramaSocial;
        public TextView tvAno;
        public TextView tvNome;
        public TextView tvSituacao;
        
        
    }  
}
